package co.sugon.log.dao;

import co.sugon.log.searchEngineService.SQLExecuteService;
import co.sugon.log.utils.MYSQLUtil;
import co.sugon.log.utils.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.Map;

public class AnalysisDatabaseDao {

    /**
     *数据库访问总次数  ip标识
     * */
    public static void dataBaseCount(String database_DateTime){

        String sql="select count(1) count,dip from  sg_wdd_result_table  where dt='" + database_DateTime + "' group by dip";
        List<Map<String, Object>> retList = SQLExecuteService.executeQuery(sql);

        //定义参数
        Connection connection =null;
        PreparedStatement pstmt =null;
        try {
            connection= MYSQLUtil.getConnection();
            //设置手动提交
            connection.setAutoCommit(false);
            String  mysqlSql="insert into xxx (x,x,x) values(?,?,?)";
            pstmt=connection.prepareStatement(mysqlSql);
            //批处理

            for (Map<String,Object> map:retList){
                pstmt.setString(1, StringUtils.isNUll(map.get("dip")));
                pstmt.setString(2,StringUtils.isNUll(map.get("count")));
                pstmt.setString(3,database_DateTime);
                pstmt.addBatch();
            }

            pstmt.executeBatch();//执行批量处理
            connection.commit();//手工提交
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            MYSQLUtil.release(connection,pstmt);
        }

    }


    /**
     *每个数据库表访问次数/类型
     * */
    public static void dataBaseTableNameCount(String database_DateTime) {

        String sql = "select count(1) count,operate_name,dip from  sg_wdd_result_table  where dt='" + database_DateTime + "' group by dip,operate_name";
        List<Map<String, Object>> retList = SQLExecuteService.executeQuery(sql);

        //定义参数
        Connection connection =null;
        PreparedStatement pstmt =null;
        try {
            connection= MYSQLUtil.getConnection();
            //设置手动提交
            connection.setAutoCommit(false);
            String  mysqlSql="insert into xxx (x,x,x) values(?,?,?,?)";
            pstmt=connection.prepareStatement(mysqlSql);
            //批处理

            for (Map<String,Object> map:retList){
                pstmt.setString(1, StringUtils.isNUll(map.get("count")));
                pstmt.setString(2,StringUtils.isNUll(map.get("operate_name")));
                pstmt.setString(3,StringUtils.isNUll(map.get("dip")));
                pstmt.setString(4,database_DateTime);
                pstmt.addBatch();
            }

            pstmt.executeBatch();//执行批量处理
            connection.commit();//手工提交
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            MYSQLUtil.release(connection,pstmt);
        }


    }


    /**
     *表操作结果分析
     * */
    public static void dataBaseResultCount(String database_DateTime) {

        String sql = "select count(1) count,operate_result,dip from  sg_wdd_result_table  where dt='" + database_DateTime + "' group by dip,operate_result";
        List<Map<String, Object>> retList = SQLExecuteService.executeQuery(sql);


        //定义参数
        Connection connection =null;
        PreparedStatement pstmt =null;
        try {
            connection= MYSQLUtil.getConnection();
            //设置手动提交
            connection.setAutoCommit(false);
            String  mysqlSql="insert into xxx (x,x,x) values(?,?,?,?)";
            pstmt=connection.prepareStatement(mysqlSql);
            //批处理

            for (Map<String,Object> map:retList){
                pstmt.setString(1, StringUtils.isNUll(map.get("count")));
                pstmt.setString(2,StringUtils.isNUll(map.get("operate_result")));
                pstmt.setString(3,StringUtils.isNUll(map.get("dip")));
                pstmt.setString(4,database_DateTime);
                pstmt.addBatch();
            }

            pstmt.executeBatch();//执行批量处理
            connection.commit();//手工提交
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            MYSQLUtil.release(connection,pstmt);
        }

    }



}
